<?php

namespace app\parent\service;

use app\common\service\BaseService;
use app\lib\exception\BaseException;
use app\models\CommentModel;
use app\models\CourseModel;
use app\models\DictionaryModel;
use app\models\GradesDetailsListsModel;
use app\models\GradesDetailsModel;
use app\models\ParentInformationModel;
use app\models\StudentModel;
use app\models\StudentSubjectInfosClassModel;
use app\models\UserModel;

class CommentService extends BaseService
{
    /**
     * User: wufeng
     * Date: 2020/03/09.
     *
     * @param $params
     *
     * @return array
     */
    public function classCommentLists($params)
    {
        try {
            $where = [];
            $student_id = (new ParentInformationModel())->get(parent_get_current_user_id())['student_id'];
            $where['campus_id'] = (new StudentModel())->get($student_id)['campus_id'];
            $where['student_id'] = $student_id;
            $class_subject_ids = (new StudentSubjectInfosClassModel())->where(['campus_id' => $where['campus_id'], 'student_id' => $student_id])->column('class_subject_id');
            $ids = [];
            foreach ($class_subject_ids as $class_subject_id) {
                $graduation_status = (new GradesDetailsModel())->get($class_subject_id)['graduation_status'];
                if (1 === (int) $graduation_status) {
                    continue;
                }
                $ids[] = $class_subject_id;
            }

            $where['class_subject_id'] = ['in', implode(',', array_unique($ids))];

            $this->data['lists'] = (new StudentSubjectInfosClassModel())
                ->where($where)
                ->select()
                ->toArray();
            foreach ($this->data['lists'] as &$list) {
                //班课类型
                $list['course_type'] = CourseModel::get(['id' => $list['course_id']])['course_type'];
                $list['course_type_text'] = DictionaryModel::get(['id' => $list['course_type']])['name'];
                $list['teacher_id'] = GradesDetailsModel::get(['id' => $list['class_subject_id']])['teacher_id'];
                $gradesDetailsLists = (new GradesDetailsListsModel())->get($list['class_manage_details_id']);
                $ids = (new GradesDetailsListsModel())->order('class_date asc')->column('id');
                $i = array_search($list['class_manage_details_id'], $ids);
                $j = $i + 1;
                $list['time_slot'] = $gradesDetailsLists['class_subject_start'].'-'.$gradesDetailsLists['class_subject_end'];
                $list['class_day'] = '第'.$j.'节';
                $list['class_date'] = $gradesDetailsLists['class_date'];
                $list['weekday'] = $gradesDetailsLists['weekday'];
                $list['headmaster'] = $gradesDetailsLists['headmaster'];
                $list['teacher'] = $gradesDetailsLists['teacher'];
                $list['assistant'] = $gradesDetailsLists['assistant'];
                $list['class_grades_name'] = $gradesDetailsLists['class_grades_name'];
                $comment_status = (new CommentModel())->get(['cmdl_id' => $list['class_manage_details_id'], 'student_id' => $student_id, 'c_type' => 1])['status'];
                if (1 === (int) $comment_status) {
                    $list['comment'] = (new CommentModel())->get(['cmdl_id' => $list['class_manage_details_id'], 'student_id' => $student_id, 'c_type' => 1]);
                    $list['comment']['picture'] = explode(',', $list['comment']['picture']);
                    $list['comment_status'] = $list['comment']['status'];
                } else {
                    $list['comment']['cmdl_id'] = null;
                    $list['comment']['student_id'] = null;
                    $list['comment']['teacher_id'] = null;
                    $list['comment']['student_scoring'] = null;
                    $list['comment']['teacher_scoring'] = null;
                    $list['comment']['share'] = null;
                    $list['comment']['comment_time'] = null;
                    $list['comment']['content'] = null;
                    $list['comment']['audio'] = null;
                    $list['comment']['audio_duration'] = null;
                    $list['comment']['video'] = null;
                    $list['comment']['picture'] = null;
                    $list['comment']['c_type'] = null;
                    $list['comment']['status'] = null;
                    $list['comment_status'] = 0;
                }
            }

            if (empty($this->data)) {
                throw new BaseException('NO_DATA');
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/11.
     *
     * @param $params
     *
     * @return array
     */
    public function classDayCommentLists($params)
    {
        try {
            $where = [];
            $student_id = (new ParentInformationModel())->get(parent_get_current_user_id())['student_id'];
            $campus_id = (new StudentModel())->get($student_id)['campus_id'];
            $class_subject_ids = (new StudentSubjectInfosClassModel())->where(['campus_id' => $campus_id, 'student_id' => $student_id])->column('class_subject_id');
            $where['id'] = ['in', implode(',', array_unique($class_subject_ids))];
            $where['graduation_status'] = 0;
            $this->data = (new GradesDetailsModel())
                ->where($where)
                ->select()
                ->toArray();
            foreach ($this->data as &$datum) {
                //班课类型
                $datum['course_type'] = (new CourseModel())->get(['id' => $datum['course_id']])['course_type'];
                $datum['course_type_text'] = (new DictionaryModel())->get(['id' => $datum['course_type']])['name'];

                //待评价课节
                $datum['stay_appraise_comment'] = (new GradesDetailsListsModel())->where(['class_manage_id' => $datum['id'], 'is_comment' => 0])->count();

                $datum['gradesDetailsLists'] = (new GradesDetailsListsModel())
                    ->where(['class_manage_id' => $datum['id']])
                    ->select()
                    ->toArray();
                $i = 0;
                foreach ($datum['gradesDetailsLists'] as &$list) {
                    $i = $i + 1;
                    $list['class_day'] = '第'.$i.'节';
                    $list['class_datetime'] = $list['class_date'].' '.$list['class_subject_start'].'-'.$list['class_subject_end'];
                    // c_type 2 老师点评学生;是否答案解析 默认为0未点评；1已点评
                    $list['teacher_status'] = (new CommentModel())->get(['cmdl_id' => $list['id'], 'c_type' => 2, 'student_id' => $student_id])['status'];
                    // c_type 1 学生点评老师;是否答案解析 默认为0未点评；1已点评
                    $list['student_status'] = (new CommentModel())->get(['cmdl_id' => $list['id'], 'c_type' => 1, 'student_id' => $student_id])['status'];
                }
            }
            if (empty($this->data)) {
                throw new BaseException('NO_DATA');
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/05.
     *
     * @param $params
     *
     * @return array
     */
    public function classDayCommentDetail($params)
    {
        try {
            $student_id = (new ParentInformationModel())->get(parent_get_current_user_id())['student_id'];
            $this->data['student'] = (new CommentModel())->get(['c_type' => 1, 'cmdl_id' => $params['cmdl_id'], 'student_id' => $student_id]);
            if ($this->data['student']) {
                $this->data['student']['student_name'] = (new StudentModel())->get($student_id)['student_name'];
            }
            $this->data['teacher'] = (new CommentModel())->get(['c_type' => 2, 'cmdl_id' => $params['cmdl_id'], 'student_id' => $student_id]);
            if ($this->data['teacher']) {
                $this->data['teacher']['teacher_name'] = (new UserModel())->get($this->data['teacher']['teacher_id'])['name'];
            }
            $gradesDetailsLists = (new GradesDetailsListsModel())->get($params['cmdl_id']);
            //班课类型
            $this->data['detail']['course_type'] = (new CourseModel())->get(['id' => $gradesDetailsLists['course_id']])['course_type'];
            $this->data['detail']['course_type_text'] = (new DictionaryModel())->get(['id' => $this->data['detail']['course_type']])['name'];
            $this->data['detail']['class_subject_start'] = $gradesDetailsLists['class_subject_start'];
            $this->data['detail']['class_subject_end'] = $gradesDetailsLists['class_subject_end'];
            $this->data['detail']['class_date'] = $gradesDetailsLists['class_date'];
            $this->data['detail']['weekday'] = $gradesDetailsLists['weekday'];
            $this->data['detail']['headmaster'] = $gradesDetailsLists['headmaster'];
            $this->data['detail']['teacher'] = $gradesDetailsLists['teacher'];
            $this->data['detail']['teacher_id'] = (new GradesDetailsModel())->get($gradesDetailsLists['class_manage_id'])['teacher_id'];
            $this->data['detail']['assistant'] = $gradesDetailsLists['assistant'];
            $this->data['detail']['class_room'] = $gradesDetailsLists['class_room'];
            $this->data['detail']['course_name'] = (new CourseModel())->get(['id' => $gradesDetailsLists['course_id']])['course_name'];
            $ids = (new GradesDetailsListsModel())->where(['class_manage_id' => $gradesDetailsLists['class_manage_id']])->order('class_date asc')->column('id');
            $i = array_search($params['cmdl_id'], $ids);
            if (($i - 1) < 0) {
                $this->data['detail']['up_day'] = -1;
            } else {
                $this->data['detail']['up_day'] = $ids[$i - 1];
            }
            if (($i + 1) >= \count($ids)) {
                $this->data['detail']['down_day'] = -1;
            } else {
                $this->data['detail']['down_day'] = $ids[$i + 1];
            }
            $j = $i + 1;
            $this->data['detail']['class_day'] = '第'.$j.'节';
            $this->data['detail']['status'] = $gradesDetailsLists['status'];
            if (empty($this->data)) {
                throw new BaseException('NO_DATA');
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * 课节点评
     * User: wufeng
     * Date: 2020/03/05.
     *
     * @param $params
     *
     * @return array
     */
    public function classDayComment($params)
    {
        try {
            $data = [];
            $data['cmdl_id'] = $params['cmdl_id'];
            $data['student_id'] = (new ParentInformationModel())->get(parent_get_current_user_id())['student_id'];
            $data['teacher_id'] = $params['teacher_id'];
            $data['student_scoring'] = $params['student_scoring'];
            $data['comment_time'] = date('Y-m-d H:i:s');
            $data['content'] = $params['content'];
            $data['audio'] = $params['audio'];
            $data['audio_duration'] = $params['audio_duration'];
            $data['video'] = $params['video'];
            $data['picture'] = $params['picture'];
            $data['c_type'] = 1;
            $data['status'] = 1;
            if (isset($params['id']) && '' !== $params['id'] && null !== $params['id']) {
                (new CommentModel())->save($data, ['id' => $params['id']]);
            } else {
                (new CommentModel())->save($data);
            }

            $count = (new CommentModel())->where(['cmdl_id' => $params['cmdl_id'], 'student_id' => $data['student_id']])->count();
            if (2 === (int) $count) {
                (new StudentSubjectInfosClassModel())->where(['class_manage_details_id' => $params['cmdl_id'], 'student_id' => $data['student_id']])->update(['is_comment' => 1]);
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }
}
